{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "——————\n",
    "\n",
    "*日期：2023年4月24日\n",
    "\n",
    "*课程：API\n",
    "\n",
    "*记录人：林锦纯\n",
    "\n",
    "——————"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "——————\n",
    "\n",
    "*百度实践\n",
    "\n",
    "*自然语言处理文本信息\n",
    "\n",
    "*自然语言生成文本信息\n",
    "\n",
    "————-——"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"text\":\"百度是一家高科技公司\",\"items\":[{\"uri\":\"\",\"formal\":\"\",\"ne\":\"ORG\",\"item\":\"百度\",\"loc_details\":[],\"basic_words\":[\"百度\"],\"byte_offset\":0,\"byte_length\":4,\"pos\":\"\"},{\"uri\":\"\",\"formal\":\"\",\"ne\":\"\",\"item\":\"是\",\"loc_details\":[],\"basic_words\":[\"是\"],\"byte_offset\":4,\"byte_length\":2,\"pos\":\"v\"},{\"uri\":\"\",\"formal\":\"\",\"ne\":\"\",\"item\":\"一家\",\"loc_details\":[],\"basic_words\":[\"一\",\"家\"],\"byte_offset\":6,\"byte_length\":4,\"pos\":\"\"},{\"uri\":\"\",\"formal\":\"\",\"ne\":\"\",\"item\":\"高科技\",\"loc_details\":[],\"basic_words\":[\"高\",\"科技\"],\"byte_offset\":10,\"byte_length\":6,\"pos\":\"n\"},{\"uri\":\"\",\"formal\":\"\",\"ne\":\"ORG\",\"item\":\"公司\",\"loc_details\":[],\"basic_words\":[\"公司\"],\"byte_offset\":16,\"byte_length\":4,\"pos\":\"\"}],\"log_id\":1650341627966598003}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "API_KEY = \"zMXMPvheWlGAd4Zoo0lDVDYy\"\n",
    "SECRET_KEY = \"hTSrnbUoLK3exSLENhezF3GgDi5hWmyO\"\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?charset=UTF-8&access_token=\" + get_access_token()\n",
    "    \n",
    "    payload = json.dumps({\n",
    "        \"text\": \"百度是一家高科技公司\"\n",
    "    })\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.text)\n",
    "    \n",
    "\n",
    "def get_access_token():\n",
    "    \"\"\"\n",
    "    使用 AK，SK 生成鉴权签名（Access Token）\n",
    "    :return: access_token，或是None(如果错误)\n",
    "    \"\"\"\n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n",
    "    params = {\"grant_type\": \"client_credentials\", \"client_id\": API_KEY, \"client_secret\": SECRET_KEY}\n",
    "    return str(requests.post(url, params=params).json().get(\"access_token\"))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"items\":[{\"prob\":0.912884,\"label\":\"neutral\",\"subitems\":[],\"replies\":[]},{\"prob\":0.0869689,\"label\":\"pessimistic\",\"subitems\":[],\"replies\":[]},{\"prob\":0.0001475,\"label\":\"optimistic\",\"subitems\":[],\"replies\":[]}],\"text\":\"今天是失落的天气\",\"log_id\":1650342858490933996}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "API_KEY = \"zMXMPvheWlGAd4Zoo0lDVDYy\"\n",
    "SECRET_KEY = \"hTSrnbUoLK3exSLENhezF3GgDi5hWmyO\"\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/nlp/v1/emotion?charset=UTF-8&access_token=\" + get_access_token()\n",
    "    \n",
    "    payload = json.dumps({\n",
    "        \"text\": \"今天是失落的天气\"\n",
    "    })\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.text)\n",
    "    \n",
    "\n",
    "def get_access_token():\n",
    "    \"\"\"\n",
    "    使用 AK，SK 生成鉴权签名（Access Token）\n",
    "    :return: access_token，或是None(如果错误)\n",
    "    \"\"\"\n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n",
    "    params = {\"grant_type\": \"client_credentials\", \"client_id\": API_KEY, \"client_secret\": SECRET_KEY}\n",
    "    return str(requests.post(url, params=params).json().get(\"access_token\"))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
